package com.ftg.learn.chapter04.service.impl;

import com.ftg.learn.chapter04.domain.DemoDomain;
import com.ftg.learn.chapter04.mapper.DemoMapper;
import com.ftg.learn.chapter04.service.DemoService;
import com.ftg.learn.chapter04.util.MybatisUtil;

import java.sql.SQLException;
import java.util.List;

/**
 * @author kangjx
 * @date 2024/1/29
 */
public class DemoServiceImpl implements DemoService {

    MybatisUtil<DemoMapper> mapperMybatisUtil = new MybatisUtil<>("demo/demo.xml");
    DemoMapper mapper = mapperMybatisUtil.getMapper(DemoMapper.class);

    @Override
    public int insertDemo(DemoDomain demo) {
        int n = 0;
        try{
            n = mapper.insertDemo(demo);
            mapperMybatisUtil.sqlCommit();
        }catch (Exception e){
            mapperMybatisUtil.sqlRollBack();
        }finally {
            mapperMybatisUtil.sqlClose();
        }
        return n;
    }

    @Override
    public List<DemoDomain> selectDemo() {
        return mapper.selectDemo();
    }

}
